<template>
  <basic-container>
    <avue-crud
    :option="option"
    :table-loading="loading"
    :data="data"
    v-model:page="page"
    v-model:search = "search"
    :permission="permissionList"
    :before-open="beforeOpen"
    v-model="form"
    ref="crud"
    @sort-change="sortChange"
    @row-update="rowUpdate"
    @row-save="rowSave" @row-del="rowDel"
    @search-change="searchChange"
    @search-reset="searchReset"
    @selection-change="selectionChange"
    @current-change="currentChange"
    @size-change="sizeChange"
    @refresh-change="refreshChange"
      @on-load="onLoad">

      <template #menu-left>
        <el-button
                type="primary"
                icon="el-icon-plus"
                v-if="permission.unit_info_add"
                @click="handleAdd"
        >新 增
        </el-button>
        <el-button type="danger"
                   icon="el-icon-delete"
                   v-if="permission.unit_info_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>

      <template #createTime-search="{ disabled, size }">
        <!--<div style="display: flex;flex-direction: start;">-->
            <!--<el-select v-model="search.createTimeRule" placeholder="比较" filterable clearable>-->
              <!--<el-option label="" value=""></el-option>-->
              <!--<el-option value=">" label="大于">大于 </el-option>-->
              <!--<el-option value=">=" label="大于等于">大于等于 </el-option>-->
              <!--<el-option value="=" label="等于">等于 </el-option>-->
              <!--<el-option value="!=" label="不等于">不等于 </el-option>-->
              <!--<el-option value="<=" label="小于等于">小于等于 </el-option>-->
              <!--<el-option value="<" label="小于">小于 </el-option>-->
            <!--</el-select>-->
            <!--<el-date-picker-->
                    <!--size="default"-->
                    <!--v-model="search.createTimeSearch"-->
                    <!--type="date"-->
                    <!--:format="dateFormat"-->
                    <!--:value-format="dateFormat"-->
                    <!--:unlink-panels="true"-->
                    <!--clearable-->
                    <!--placeholder="创建日期"-->
                    <!--@change="handleDatePickerChange"-->
                    <!--:picker-options="pickerOptions"-->
            <!--&gt;-->
            <!--</el-date-picker>-->
          <!--</div>-->
        <el-date-picker
        size="default"
        style="width:94%"
        v-model="this.createTimeRange"
        type="daterange"
        :format="dateFormat"
        :value-format="dateFormat"
        unlink-panels
        start-placeholder="Start Date"
        end-placeholder="End Date"
        clearable
        placeholder="创建日期"
        @change="handleDatePickerChange"
        :picker-options="pickerOptions"
        >
        </el-date-picker>
      </template>

      <template #menu="{ row, size, type }">
        <el-tooltip content="查看" placement="top">
          <el-button
                  icon="el-icon-view"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleView(row)"
                  v-if="permission.unit_info_view"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="修改" placement="top">
          <el-button
                  icon="el-icon-edit"
                  link
                  :size="size"
                  :type="type"
                  @click="() => handleEdit(row)"
                  v-if="permission.unit_info_edit"
          >
          </el-button>
        </el-tooltip>
        <el-tooltip content="删除" placement="top">
          <el-button
                  icon="el-icon-delete"
                  link
                  :size="size"
                  :type="type"
                  @click="() => rowDel(row)"
                  v-if="permission.unit_info_delete"
          >
          </el-button>
        </el-tooltip>
      </template>
    </avue-crud>
    <unit-info-form ref="unitInfoForm" @success="refreshChange"></unit-info-form>
  </basic-container>
</template>

<script>
import { getList, getDetail, add, update, remove } from "@/api/anwise/unit_info";
import { mapGetters } from "vuex";
import unitInfoForm from "@/views/anwise/setting/unit_info_form.vue";
export default {
  components: {
    unitInfoForm,
  },
  data() {
    return {
      form: {},
      query: {},
        createTimeRange:[],
      loading: true,
      dateFormat: 'YYYY-MM-DD',
      pickerOptions: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      search: {},
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        addBtn: false,
        editBtn: false,
          indexWidth:100,
        delBtn: false,
        stripe: true,
        searchIndex: 3,
        searchIcon: false,
        searchShow: false,
        searchMenuSpan: 6,
          searchEnter:true,
        column: [
          {
            label: "",
            prop: "id",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
              overHidden: true,
              sortable: true,
            hide: true,
          },
          {
            label: "单位名称",
            prop: "name",
            type: "input",
            search: true,
              overHidden: true,
              sortable: true,
            rules: [{
              required: true,
              message: "请输入单位名称",
              trigger: "blur"
            }],
          },
            {
                label: "单位类型",
                prop: "type",
                type: "select",
                dicUrl: "/blade-system/dict-biz/dictionary?code=unit_info_type",
                filterable: true,
                overHidden: true,
                search: true,
                sortable: true,
                width:120,
                props: {
                    label: 'dictValue',
                    value: 'dictKey',
                },
                dataType: 'string',
                rules: [{
                    required: false,
                    message: "请输入单位类型",
                    trigger: "blur"
                }],
            },
          {
            label: "备注",
            prop: "remark",
            sortable: true,
              overHidden: true,
            type: "input",
          },
          {
            label: "创建人员",
            prop: "createUserName",
            sortable: true,
              overHidden: true,
            type: "input",
          },
          {
            label: "创建时间",
            prop: "createTime",
              overHidden: true,
              // searchSpan:12,
            sortable: true,
            type: "input",
            search: true,
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.validData(this.permission.unit_info_add, false),
        viewBtn: this.validData(this.permission.unit_info_view, false),
        delBtn: this.validData(this.permission.unit_info_delete, false),
        editBtn: this.validData(this.permission.unit_info_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    handleView(row) {
      let title = "查看单位信息";
      let type = 'view';
      const id = row.id;
      this.$refs.unitInfoForm.show({
        id,
        title,
        type
      })
    },
    handleEdit(row) {
      let title = "编辑单位信息";
      let type = 'edit';
      const id = row.id;
      this.$refs.unitInfoForm.show({
        id,
        title,
        type
      })
    },
    handleAdd(){
      let title="创建单位信息";
      let type='edit';
      this.$refs.unitInfoForm.show({
          title,
          type
      })
    },
    sortChange(val) {
      console.log(val)
      this.sort = {
        prop: val.prop,
        order: val.order
      };
      let order = "";
      if (this.sort.prop && this.sort.order) {
        if (this.sort.order === "descending") {
          order = `${this.sort.prop} desc`
        } else {
          order = `${this.sort.prop} asc`
        }
      }
      this.query.orderBy = order;
      console.log( this.query)
      this.onLoad(this.page);
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
      searchReset() {
          this.query = {};
          this.createTimeRange = [];
          this.search.createTimeStart = '';
          this.search.createTimeEnd = '';
          this.onLoad(this.page);
      },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleDatePickerChange(value) {
        console.log(value);
        if (value) {
            this.search.createTimeStart = value[0];
            this.search.createTimeEnd = value[1];
        } else {
            this.search.createTimeStart = '';
            this.search.createTimeEnd = '';
        }
    },
  }
};
</script>

<style></style>
